package ps.client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import ps.client.gui.util.textpanel.ChatLineFormater;
import ps.client.gui.util.textpanel.DefaultChatLineFormater;
import ps.client.gui.util.textpanel.DefaultMessageLineFormater;
import ps.client.gui.util.textpanel.MessageLineFormater;
import ps.client.gui.util.textpanel.TextPanel;


@SuppressWarnings("serial")
public class MessagePanel extends JPanel {
    
    private static final Color COLOR_BG = Color.BLACK;
    
    private static final Color MESSAGE_COLOR = new Color(150, 150, 255);
    private static final int MESSAGE_SIZE = 11;
    
    TextPanel textPanel = new TextPanel();
    JScrollPane msgAreaScrollPane = new JScrollPane(textPanel);
    
    ChatLineFormater chatLineFormater = new DefaultChatLineFormater();
    MessageLineFormater messageLineFormater = new DefaultMessageLineFormater();
    
    MainFrame mainFrame;
    
    public MessagePanel(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        
        setBackground(COLOR_BG);
        setBorder(new EmptyBorder(3, 3, 3, 3));
        setLayout(new BorderLayout());
        add(msgAreaScrollPane, BorderLayout.CENTER);
        msgAreaScrollPane.setBorder(null);
        msgAreaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        msgAreaScrollPane.getVerticalScrollBar().setPreferredSize(
                new Dimension(14, Integer.MAX_VALUE));
        msgAreaScrollPane.setBackground(Color.BLACK);
        msgAreaScrollPane.getVerticalScrollBar().setUnitIncrement(16);
        
        textPanel.setBackground(Color.BLACK);
        
        textPanel.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                msgAreaScrollPane.getViewport().scrollRectToVisible(
                        new Rectangle(0, e.getComponent().getHeight(), 0, 0));
            }
        });
    }
    
    public void clearMessages() {
        textPanel.clearText();
    }
    
    public void appendTextMsg(String msg) {
        appendTextMsg(msg, MESSAGE_SIZE, MESSAGE_COLOR);
    }
    
    public void appendTextMsg(String msg, int size, Color color) {
        textPanel.appendLine(messageLineFormater.formatMessageLine(msg, size, color));
    }
    
    public void appendChatMsg(String sender, String reciever, String msg) {
        textPanel.appendLine(chatLineFormater.formatChatLine(sender, reciever, msg));
    }
    
    public void setMessageLineFormater(MessageLineFormater messageLineFormater) {
        this.messageLineFormater = messageLineFormater;
    }
    
    public void setChatLineFormater(ChatLineFormater chatLineFormater) {
        this.chatLineFormater = chatLineFormater;
    }
    
    public void setTimeStamp(boolean b) {
        messageLineFormater.setTimeStamp(b);
        chatLineFormater.setTimeStamp(b);
    }

    public ChatLineFormater getChatLineFormater() {
        return chatLineFormater;
    }

    
    public MessageLineFormater getMessageLineFormater() {
        return messageLineFormater;
    }
    
    

}
